//
//  CinemaViewController.m
//  Movie
//
//  Created by tengyang on 15/7/17.
//  Copyright (c) 2015年 tengyang. All rights reserved.
//

#import "CinemaViewController.h"
#import "starView.h"
#import "DataService.h"
#import "CinemaModal.h"
#import "CinemaCell.h"

#define kWidth self.view.bounds.size.width
#define kHeight self.view.bounds.size.height

@interface CinemaViewController ()
{
    NSInteger tempIndex;
    
}
@end

@implementation CinemaViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title=@"影院";
        
    }
    return self;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self _createJsob];
    
    [self _createTableView];
    tempIndex=0;
}



- (void)_loadJson
{

    _cinemaArray=[[NSMutableArray alloc]init];
    for (int i=0; i<19; i++) {
       NSDictionary *dic1=_newArray[i];
        NSArray *array=[dic1 objectForKey:@"cinema"];
        for (NSDictionary *temp in array) {
            CinemaModal *modal=[[CinemaModal alloc]init];
            modal.address=[temp objectForKey:@"address"];
            modal.circleName=[temp objectForKey:@"circleName"];
            modal.grade=[temp objectForKey:@"grade"];
            modal.isCouponSupport=[temp objectForKey:@"isCouponSupport"];
            modal.isSeatSupport=[temp objectForKey:@"isSeatSupport"];
            modal.lowPrice=[temp objectForKey:@"lowPrice"];
            [_cinemaArray addObject:modal];
        }
        
    }
    
    
}

- (void)_createJsob
{
    
    NSDictionary *temp=[DataService getJsonDataFromFile:@"district_list.json"];
    _districtList=[temp objectForKey:@"districtList"];
    NSDictionary *temp1=[DataService getJsonDataFromFile:@"cinema_list.json"];
    
    NSArray *cinemaList =[temp1 objectForKey:@"cinemaList"];
    _newArray=[[NSMutableArray alloc]init];
    for (int i=0; i<19; i++) {
        NSMutableDictionary *newDic=[[NSMutableDictionary alloc]init];
        NSString *id=[_districtList[i] objectForKey:@"id"];
        NSString *name=[_districtList[i] objectForKey:@"name"];
        NSMutableArray *tempArray=[[NSMutableArray alloc]init];
        
        for (int j=0; j<142; j++) {
            NSDictionary *dic=cinemaList[j];
            if ([id isEqualToString:[dic objectForKey:@"districtId"]]) {
                [tempArray addObject:dic];
                
            }
            
        }
        
        [newDic setObject:name forKey:@"name"];
        
        [newDic setObject:tempArray forKey:@"cinema"];
        
        [_newArray addObject:newDic];
           }
   // NSLog(@"%@",_newArray);

    
}

- (void)_createTableView
{
    _cinemaTableView =[[UITableView alloc]initWithFrame:self.view.bounds];
    [self.view addSubview:_cinemaTableView];
    _cinemaTableView.dataSource=self;
    _cinemaTableView.delegate=self;
    _cinemaTableView.sectionHeaderHeight=40;
    
    UINib *nib=[UINib nibWithNibName:@"CinemaCell" bundle:[NSBundle mainBundle]];
    [_cinemaTableView registerNib:nib forCellReuseIdentifier:@"cell"];
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CinemaCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    
    NSArray *array=[_newArray[indexPath.section] objectForKey:@"cinema"];
    NSDictionary *dic=array[indexPath.row];
    
    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight)];
    UILabel *address=[[UILabel alloc]initWithFrame:CGRectMake(0, 40, 300, 20)];
    UILabel *name=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 250, 40)];
    UILabel *grade=[[UILabel alloc]initWithFrame:CGRectMake(330, 0, 40, 20)];
    
    UILabel *price=[[UILabel alloc]initWithFrame:CGRectMake(300, 30, 60, 30)];
    
    UILabel *kim=[[UILabel alloc]initWithFrame:CGRectMake(300, 70, 60, 20)];
    kim.textColor=[UIColor whiteColor];
    kim.text=@"10KM";
//    price.text=[dic objectForKey:@"lowPrice"];
//    if ([price isKindOfClass:[NSNull class]]) {
//        price.text=@"price";
//    }
//    price.textColor=[UIColor whiteColor];
    price.text=@"￥40";
    //price.text=[dic objectForKey:@"lowPrice"];
    price.textColor=[UIColor orangeColor];
    name.text=[dic objectForKey:@"name"];
    name.font=[UIFont systemFontOfSize:20];
    address.text=[dic objectForKey:@"address"];
    address.font=[UIFont systemFontOfSize:14];
    grade.text=[dic objectForKey:@"grade"];
    name.textColor=[UIColor whiteColor];
    address.textColor=[UIColor whiteColor];
    grade.textColor=[UIColor orangeColor];
    
    
    NSString *temp=[dic objectForKey:@"isCouponSupport"];
    NSInteger temp1=temp.integerValue;
    if (temp1==1) {
        UILabel *coupon=[[UILabel alloc]initWithFrame:CGRectMake(0, 70, 30, 20)];
        coupon.text=@"券";
        coupon.textColor=[UIColor whiteColor];
        //coupon.backgroundColor=[]
        [view addSubview:coupon];
        price.text=@"￥45";
    }
    
    NSString *temp2=[dic objectForKey:@"isSeatSupport"];
    NSInteger temp3=temp2.integerValue;
    if (temp3==1) {
        UILabel *seat=[[UILabel alloc]initWithFrame:CGRectMake(30, 70, 30, 20)];
        seat.text=@"座";
        seat.textColor=[UIColor redColor];
        [view addSubview:seat];
        
    }
    
    
    [view addSubview:address];
    [view addSubview:name];
    [view addSubview:grade];
    [view addSubview:kim];
    [view addSubview:price];
    view.backgroundColor=[UIColor blackColor];
    cell.backgroundView=view;
    

    return cell;
    
    
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 90;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (flags[section]==NO) {
        return 0;
    }
    
    NSDictionary *dic=_newArray[section];
    NSArray *array=[dic objectForKey:@"cinema"];
    NSLog(@"%li",array.count);
    return array.count;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
   
    return _newArray.count;
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, kWidth, 40)];
    [button setBackgroundImage:[UIImage imageNamed:@"navbar_bg_normal"] forState:UIControlStateNormal];
    //button.backgroundColor=[UIColor blackColor];
    NSDictionary *temp=_newArray[section];
    NSString *str=[temp objectForKey:@"name"];
    [button setTitle:str forState:UIControlStateNormal];
    button.tag=section;
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    return button;
    
}

- (void)buttonAction:(UIButton *)button
{
    NSInteger index=button.tag;
    flags[index]=!flags[index];
    [_cinemaTableView reloadData];
}










- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
